/**
 * Created by 威哥 on 2017/7/5.
 */


(function (window) {

    var  $ =  window.jQuery

    window.mmm = {
        queryStr:queryStr,
        setAjax:setAjax,
        fanye:fanye,
        Top:Top
    }

    function queryStr(url) {
        var obj = {},
            str = url.split('?')[1]
        arr = str.split('&');
        arr.forEach(function (value, index) {
            var key = value.split("=")[0],
                value = value.split("=")[1];
            obj[key] = value;
        })
        return obj;
    }

    function setAjax (url,cobaike,data){
        data = data || {};
        $.ajax({
            url:'http://139.199.192.48:9090/api/'+url+'',
            type:'get',
            dataType:'json',
            data:data,
            success:function (rl) {
                if(cobaike && typeof cobaike === 'function') {
                    cobaike(rl)
                }
            }
        })
    }

    function fanye (rl,url,newStr) {
        newStr = newStr || null
        var ye = 1,
            i = 0,
            optionStr = '',
            totalCount = rl.totalCount,
            pagesize = rl.pagesize,
            Maxpagesize = Math.ceil(totalCount/pagesize)

        for (; i < Maxpagesize; i++ ) {
            optionStr += '<option>'+(i+1)+'/'+Maxpagesize+'</option>'
        }
        $('#selectPage').html(optionStr);


        $('#selectPage ').on('change',function () {
            $('#selectPage > option').each(function (index,el) {
                if($(el).prop('selected') === true){
                    ye = index + 1;
                    getData();
                }
            })
        })


        $('.xia').on('click',function () {
            if(ye >= Maxpagesize) {
                return
            }
            ye++;
            $('#selectPage').children().eq(ye - 1).prop('selected', true);

            getData();
        })


        $('.shang').on('click',function () {

            if(ye <= 1) {
                return
            }
            ye--;
            $('#selectPage').children().eq(ye - 1).prop('selected', true);

            getData();

        })

        function  getData () {
            $.ajax({
                url: 'http://139.199.192.48:9090/api/'+url,
                type: 'get',
                dataType: 'json',
                data: {
                    categoryid: newStr,
                    pageid:ye
                },
                success: function (rl) {
                    var htmlStr = template('tip2',rl)
                    $('.product-list > ul').html(htmlStr)
                }
            })
        }
    }
  //页面滚动
    function Top (el) {
        el = el || '#top'
        $(el).on('click',function () {
            console.log(123);
            $("body,html").stop().animate({scrollTop:0},1000)
        })
    }

})(window);